package com.itheima.web;

import com.github.pagehelper.PageInfo;
import com.itheima.daomain.User;
import com.itheima.service.*;
import com.itheima.util.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @Descriptionn:test
 * @Create by:依然
 * @Date:2021/11/4 16:55
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {

    public void querAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = new UserService();
        PageInfo<User> userPageInfo = userService.queryAllUserServlet();
        request.setAttribute("pageInfo", userPageInfo);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }

    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        addUserService addUserService = new addUserService();
        Integer integer = addUserService.addUser(user);
        if (integer > 0) {
            response.sendRedirect("/add_ok.jsp");
        } else {
            PrintWriter pw = response.getWriter();

            pw.print("<script>");

            pw.print("alert('添加失败,请重新添加');");
            pw.print("location.href='http://127.0.0.1:8080/add.jsp';");

            pw.print("</script>");
        }
    }

    public void deleteUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        deleteDServlet dServlet = new deleteDServlet();
        String id = request.getParameter("id");
        Integer deleteid = dServlet.deleteid(Integer.parseInt(id));
        if (deleteid > 0) {
            response.sendRedirect("/delete_ok.jsp");
        } else {

            System.out.println("删除失败");
        }
    }

    public void querUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        updateServlet updateServlet = new updateServlet();
        String id = request.getParameter("id");
        User user = updateServlet.update(Integer.parseInt(id));
        System.out.println("获取到的ID:" + id);
        request.setAttribute("users", user);
        request.getRequestDispatcher("/update.jsp").forward(request, response);
    }

    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        updateUserServlet userServlet = new updateUserServlet();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(user);
        Integer updateuser = userServlet.updateuser(user);
        if (updateuser > 0) {
            response.sendRedirect("/update_ok.jsp");
        } else {
            System.out.println("修改失败");
        }
    }

    public void userPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number = request.getParameter("number");
        //String number=null;
        if (number == null) {
            number = String.valueOf(1);
        }
        System.out.println("Userpage:" + number);
        UserpageService userpage = new UserpageService();
        List<User> pages = userpage.pages(Integer.parseInt(number));
        System.out.println("web里的输出" + pages);
        request.setAttribute("users", pages);
        request.setAttribute("id", number);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }

    public void searchUserByKey(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SelectLike selectLike = new SelectLike();
        String keyword = request.getParameter("keyword");
        System.out.println(keyword);

        /*for (User user : key) {
            System.out.println("searchUserByKey"+ user);
        }*/

        String currentPage = request.getParameter("currentPage");
        if(currentPage==null){
            currentPage= String.valueOf(1);
        }

        PageInfo<User> userBySearchKey = selectLike.findUserBySearchKey(Integer.parseInt(currentPage), keyword);



            request.setAttribute("pageInfo",userBySearchKey);
            request.getRequestDispatcher("/list.jsp").forward(request, response);

           /* //没有数据： 给出提示：暂无数据
            response.getWriter().print("<h3>暂无数据</h3>");*/

    }

    public void pagehelp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取请求中的数据
        String currentPage = request.getParameter("currentPage");
        //定义当前页码变量
        int curPage = 1;

        if (currentPage != null) {
            curPage = Integer.parseInt(currentPage);
        }
        UserService userService = new UserService();
        PageInfo<User> pageInfo = userService.pageUser(curPage);
        System.out.println(pageInfo.getPages());
        request.setAttribute("pageInfo", pageInfo);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }
}
